﻿using System.Text;
using System.Xml;
using MicroStar.Patterns;
using Microsoft.Xna.Framework.Graphics;

#if WINDOWS
using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Intermediate;
#endif

namespace MicroStar
{
    public class ParticleEffectFactory
    {
        private readonly ParticleEffect particleEffect;

        public static ParticleEffectFactory Initialize(int maxParticles, int particleLifespan)
        {
            ParticleEffect particleEffect = new ParticleEffect(maxParticles, particleLifespan);
            return new ParticleEffectFactory(particleEffect);
        }

        private ParticleEffectFactory(ParticleEffect particleEffect)
        {
            this.particleEffect = particleEffect;
        }

        public ParticleEffectFactory SetEmitAmount(int emitAmount)
        {
            particleEffect.EmitAmount = emitAmount;
            return this;
        }

        public ParticleEffectFactory SetMaxParticleSpeed(float maxSpeed)
        {
            particleEffect.MaxParticleSpeed = maxSpeed;
            return this;
        }

        public ParticleEffectFactory SetEmissionPattern(IPattern emissionPattern)
        {
            particleEffect.EmissionPattern = emissionPattern;
            return this;
        }

        public ParticleEffectFactory AddTexture(Texture2D texture)
        {
            particleEffect.Textures.Add(texture);
            return this;
        }

        public ParticleEffectFactory AddModifier(IModifier modifier)
        {
            particleEffect.Modifiers.Add(modifier);
            return this;
        }

        public ParticleEffect Create()
        {
            return particleEffect;
        }

#if WINDOWS
        public ParticleEffectFactory ExportConfiguration(string xmlOutputPath)
        {
            XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlOutputPath, Encoding.UTF8);
            xmlTextWriter.Formatting = Formatting.Indented;
            IntermediateSerializer.Serialize(xmlTextWriter, particleEffect, "ParticleEffect");
            xmlTextWriter.Flush();

            return this;
        }
#endif
    }
}